package com.bootdo.clouddo.controller.article;

import com.bootdo.clouddo.controller.common.CommonController;
import com.bootdo.clouddo.domain.article.ArticleDO;
import com.bootdo.clouddo.domain.juejin.ArticleCollectionDO;
import com.bootdo.clouddo.dto.juejin.RequestPostDTO;
import com.bootdo.clouddo.service.article.IArticleDetailService;
import com.bootdo.clouddo.service.article.IArticleService;
import com.bootdo.clouddo.service.juejin.*;
import com.bootdo.clouddo.context.FilterContextHandler;
import com.bootdo.clouddo.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController extends CommonController {
	
	@Autowired
	private IArticleService articleService;
	
	@Autowired
	private IArticleDetailService detailService;
	
	@Autowired
	private IArticleCollectionService collectionService;
	
	@GetMapping("/find")
	public R find(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="12")Integer size, RequestPostDTO params) {
		PageHelper.startPage(page, size);
		return R.data(new PageInfo<>(articleService.find(params.getCategory(), params.getWd(), params.getChannel(), page, size)));
	}

	@GetMapping("/detail/{id}")
	public R detail(@PathVariable String id) {
		ArticleDO article = articleService.get(id);
		if(article == null)
			return R.error("article_not_found");
		article.setDetail(detailService.get(id));
		return R.data(article);
	}
	
	@GetMapping("/findByUser")
	public R findByUser(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="12")Integer size, RequestPostDTO params) {
		PageHelper.startPage(page, size);
		return R.data(new PageInfo<>(articleService.findByUser(params.getCategory(), params.getWd(), params.getChannel(), page, size)));
	}
	
	@GetMapping("/countByUser")
	public R countByUser(@Param("uid")String uid){
		Map<String, ?> data = articleService.countByUser(uid);
		return R.data(data);
	}

	@GetMapping("/info/{id}")
	public R info(@PathVariable String id) {
		//查询收藏信息
		ArticleCollectionDO collection = collectionService.findByUidAndAid(getUid(), id);
		return R.ok().put("collection", collection != null ? true : false);
	}
	
}
